package com.neuedu.myapplication;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.util.Log;

import com.google.gson.Gson;
import com.neuedu.myapplication.databinding.ActivityWeatherListBinding;

import java.io.IOException;
import java.util.List;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

public class WeatherListActivity extends AppCompatActivity {
    ActivityWeatherListBinding binding;

    private String cityName = "";

    private WeatherCityNameReceiver receiver;

    private WeatherListAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = ActivityWeatherListBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());

        //注册广播接收器
        receiver = new WeatherCityNameReceiver();
        IntentFilter filter = new IntentFilter("android.intent.weatherCityName");
        this.registerReceiver(receiver,filter);

        binding.rv.setLayoutManager(new LinearLayoutManager(WeatherListActivity.this,
                RecyclerView.HORIZONTAL,false));

        adapter = new WeatherListAdapter(WeatherListActivity.this);
        binding.rv.setAdapter(adapter);

        getListData("北京市");
    }

    private void getListData(String cityName) {
        String token = SPManagerUtils.getToken(WeatherListActivity.this);
        String url = WeatherOkHttp.weatherList + cityName;

        OkHttpClient okHttpClient = new OkHttpClient();

        Request request = new Request.Builder()
                .url(url)
                .addHeader("Authorization",token)
                .get()
                .build();

        Call call = okHttpClient.newCall(request);

        call.enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {

            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
//                Log.i("onSuccess", "onResponse: " + response.body().string());
                WeatherListBean bean = new Gson().fromJson(WeatherOkHttp.getSuccess(response).toString(),
                        WeatherListBean.class);
                if (bean != null){
                    List<WeatherListBean.DataBean> dataBeans = bean.getData();
                    if (dataBeans != null && dataBeans.size() > 0){
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                adapter.clear();
                                adapter.setDataBeans(dataBeans);
                            }
                        });

                    }
                }
            }
        });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        this.unregisterReceiver(receiver);
    }

    public class WeatherCityNameReceiver extends BroadcastReceiver{

        @Override
        public void onReceive(Context context, Intent intent) {
            if (intent.getAction().equals("android.intent.weatherCityName")){
                cityName = intent.getStringExtra("cityName");

                binding.tvName.setText(cityName);

                getListData(cityName);
            }
        }
    }

}